<?php

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use PhpOffice\PhpSpreadsheet\IOFactory;

class ReadClient extends Command
{

    protected function configure()
    {
        $this->setName('ReadClient')
            ->setDescription('将Excel中的客户表读入到数据库');
    }

    protected function execute(Input $input, Output $output)
    {
        $filePath = APP_PATH . '../demofiles/clientdemo.cvs'; //excel文件路径
        //$filePath = "/www/wwwroot/zhongxiang/demofiles/clientdemo.csv";
        $output->writeln($filePath);

        try {
            $sheet = IOFactory::load($filePath);
            var_dump($sheet);
            $worksheet = $sheet->getActiveSheet();
            $highestRow = $worksheet->getHighestRow();
            $highestColumn = $worksheet->getHighestColumn();

            for ($row = 1; $row <= $highestRow; $row++){
                $rowData = [];
                for ($col = 'A'; $col <= $highestColumn; $col++)
                {
                    $cellValue = $worksheet->getCell($col.$row)->getValue();
                    $rowData[] = $cellValue;
                }
                $output->writeln(implode(', ', $rowData));
            }
        }catch (\Exception $e){
            $output->writeln('读取Excel文件错误：'.$e->getMessage());
        }
        $output->info("ReadClient Build Successed!");
    }

}
